//
//  StringExtension.swift
//  xz-app
//
//  Created by chen on 2025/5/6.
//
import SwiftUI
import CryptoKit

extension String {
    var md5: String {
        let computed = Insecure.MD5.hash(data: self.data(using: .utf8)!)
        return computed.map { String(format: "%02hhx", $0) }.joined()
    }
    
    // Base64 编码
    func toBase64() -> String {
        return Data(self.utf8).base64EncodedString()
    }
    
    // Base64 解码
    func fromBase64() -> String? {
        guard let data = Data(base64Encoded: self) else { return nil }
        return String(data: data, encoding: .utf8)
    }
    
    func maskPhoneNumber() -> String {
        guard self.count == 11 else { return self }
        let prefix = self.prefix(3)
        let suffix = self.suffix(4)
        return prefix + "****" + suffix
    }
}

